<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="sku_id" prop="skuId">
        <el-input
          v-model="queryParams.skuId"
          placeholder="请输入sku_id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商家" prop="supplierId">
        <el-select v-model="queryParams.supplierId" placeholder="请选择商家" clearable>
          <el-option
            v-for="dict in dict.type.temu_supplier_id"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="spu_id" prop="spuId">
        <el-input
          v-model="queryParams.spuId"
          placeholder="请输入spu_id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="skc_id" prop="skcId">
        <el-input
          v-model="queryParams.skcId"
          placeholder="请输入skc_id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商家价格" prop="supplierPrice">
        <el-input
          v-model="queryParams.supplierPrice"
          placeholder="请输入商家价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="货号" prop="extCode">
        <el-input
          v-model="queryParams.extCode"
          placeholder="请输入货号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="宽(mm)" prop="width">
        <el-input
          v-model="queryParams.width"
          placeholder="请输入宽(mm)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="高(mm)" prop="height">
        <el-input
          v-model="queryParams.height"
          placeholder="请输入高(mm)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="重量(mg)" prop="weight">
        <el-input
          v-model="queryParams.weight"
          placeholder="请输入重量(mg)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库存类型" prop="stockType">
        <el-select v-model="queryParams.stockType" placeholder="请选择库存类型" clearable>
          <el-option
            v-for="dict in dict.type.temu_stock_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="库存数量" prop="stockQuantity">
        <el-input
          v-model="queryParams.stockQuantity"
          placeholder="请输入库存数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="冻结库存数量" prop="frozenStockQuantity">
        <el-input
          v-model="queryParams.frozenStockQuantity"
          placeholder="请输入冻结库存数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['temu:sku:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['temu:sku:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['temu:sku:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['temu:sku:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="skuList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="sku_id" align="center" prop="skuId" />
      <el-table-column label="商家" align="center" prop="supplierId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.temu_supplier_id" :value="scope.row.supplierId"/>
        </template>
      </el-table-column>
      <el-table-column label="spu_id" align="center" prop="spuId" />
      <el-table-column label="skc_id" align="center" prop="skcId" />
      <el-table-column label="产品属性列表" align="center" prop="productPropertyList" >
        <template slot-scope="scope">
          <div v-html="formatProductPropertyList(scope.row)">
          </div>
        </template>
      </el-table-column>
      <el-table-column label="商家价格" align="center" prop="supplierPrice" />
      <el-table-column label="货号" align="center" prop="extCode" />
      <el-table-column label="长(mm)" align="center" prop="len" />
      <el-table-column label="宽(mm)" align="center" prop="width" />
      <el-table-column label="高(mm)" align="center" prop="height" />
      <el-table-column label="重量(mg)" align="center" prop="weight" />
      <el-table-column label="预览图" align="center" prop="skuPreviewImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.skuPreviewImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="购买链接" align="center" prop="buyUrl" >
        <template slot-scope="scope">
          <a :href="scope.row.buyUrl" target="_blank" class="buttonText">点击跳转</a>
        </template>
      </el-table-column>
      <el-table-column label="库存类型" align="center" prop="stockType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.temu_stock_type" :value="scope.row.stockType"/>
        </template>
      </el-table-column>
      <el-table-column label="库存数量" align="center" prop="stockQuantity" />
      <el-table-column label="冻结库存数量" align="center" prop="frozenStockQuantity" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['temu:sku:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['temu:sku:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改sku对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商家" prop="supplierId">
          <el-select v-model="form.supplierId" placeholder="请选择商家">
            <el-option
              v-for="dict in dict.type.temu_supplier_id"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="spu_id" prop="spuId">
          <el-input v-model="form.spuId" placeholder="请输入spu_id" />
        </el-form-item>
        <el-form-item label="skc_id" prop="skcId">
          <el-input v-model="form.skcId" placeholder="请输入skc_id" />
        </el-form-item>
        <el-form-item label="商家价格" prop="supplierPrice">
          <el-input v-model="form.supplierPrice" placeholder="请输入商家价格" />
        </el-form-item>
        <el-form-item label="货号" prop="extCode">
          <el-input v-model="form.extCode" placeholder="请输入货号" />
        </el-form-item>
        <el-form-item label="宽(mm)" prop="width">
          <el-input v-model="form.width" placeholder="请输入宽(mm)" />
        </el-form-item>
        <el-form-item label="高(mm)" prop="height">
          <el-input v-model="form.height" placeholder="请输入高(mm)" />
        </el-form-item>
        <el-form-item label="重量(mg)" prop="weight">
          <el-input v-model="form.weight" placeholder="请输入重量(mg)" />
        </el-form-item>
        <el-form-item label="预览图" prop="skuPreviewImage">
          <image-upload v-model="form.skuPreviewImage"/>
        </el-form-item>
        <el-form-item label="购买链接" prop="buyUrl">
          <el-input v-model="form.buyUrl" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="库存类型" prop="stockType">
          <el-select v-model="form.stockType" placeholder="请选择库存类型">
            <el-option
              v-for="dict in dict.type.temu_stock_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="库存数量" prop="stockQuantity">
          <el-input v-model="form.stockQuantity" placeholder="请输入库存数量" />
        </el-form-item>
        <el-form-item label="冻结库存数量" prop="frozenStockQuantity">
          <el-input v-model="form.frozenStockQuantity" placeholder="请输入冻结库存数量" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSku, getSku, delSku, addSku, updateSku } from "@/api/temu/sku";

export default {
  name: "Sku",
  dicts: ['temu_stock_type', 'temu_supplier_id'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // sku表格数据
      skuList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        skuId: null,
        supplierId: null,
        spuId: null,
        skcId: null,
        productPropertyList: null,
        supplierPrice: null,
        extCode: null,
        len: null,
        width: null,
        height: null,
        weight: null,
        skuPreviewImage: null,
        buyUrl: null,
        stockType: null,
        stockQuantity: null,
        frozenStockQuantity: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        spuId: [
          { required: true, message: "spu_id不能为空", trigger: "blur" }
        ],
        skcId: [
          { required: true, message: "skc_id不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询sku列表 */
    getList() {
      this.loading = true;
      listSku(this.queryParams).then(response => {
        this.skuList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        skuId: null,
        supplierId: null,
        spuId: null,
        skcId: null,
        productPropertyList: null,
        supplierPrice: null,
        extCode: null,
        len: null,
        width: null,
        height: null,
        weight: null,
        skuPreviewImage: null,
        buyUrl: null,
        stockType: null,
        stockQuantity: null,
        frozenStockQuantity: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.skuId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加sku";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const skuId = row.skuId || this.ids
      getSku(skuId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改sku";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.skuId != null) {
            updateSku(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSku(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const skuIds = row.skuId || this.ids;
      this.$modal.confirm('是否确认删除sku编号为"' + skuIds + '"的数据项？').then(function() {
        return delSku(skuIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('temu/sku/export', {
        ...this.queryParams
      }, `sku_${new Date().getTime()}.xlsx`)
    },
    formatProductPropertyList(row){
      var propertyList = row.productPropertyList;
      var arr = JSON.parse(propertyList);
      console.log(arr)

      var result = '';

      for (let i = 0; i < arr.length; i++) {
        console.log(arr[i].name + ':' + arr[i].value)
        result += (arr[i].name + ':' + arr[i].value + '<br>');
      }

      return result;
    }
  }
};
</script>
